// 定义弹窗管理器的类型接口
interface IPopupManager {
  show(content: string): void;
  hide(): void;
}

class PopupManager implements IPopupManager {
  // 静态属性：存储唯一实例
  private static instance: PopupManager | null = null;
  // 私有属性：存储弹窗DOM
  private popupDOM: HTMLDivElement | null = null;

  // 私有构造函数：禁止外部通过new创建实例
  private constructor() {
    this.popupDOM = this.createPopupDOM();
  }

  // 私有方法：创建弹窗DOM
  private createPopupDOM(): HTMLDivElement {
    const div = document.createElement('div');
    div.style.cssText = `
      position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);
      padding: 20px; background: #fff; border: 1px solid #eee; border-radius: 4px;
      z-index: 9999; display: none;
    `;
    document.body.appendChild(div);
    return div;
  }

  // 静态方法：全局访问入口
  public static getInstance(): PopupManager {
    if (!PopupManager.instance) {
      PopupManager.instance = new PopupManager();
    }
    return PopupManager.instance;
  }

  // 显示弹窗（实现IPopupManager接口）
  public show(content: string): void {
    if (this.popupDOM) {
      this.popupDOM.innerText = content;
      this.popupDOM.style.display = 'block';
    }
  }

  // 隐藏弹窗（实现IPopupManager接口）
  public hide(): void {
    if (this.popupDOM) {
      this.popupDOM.style.display = 'none';
    }
  }
}

// 测试：TypeScript类型安全的单例调用
const popup1 = PopupManager.getInstance();
const popup2 = PopupManager.getInstance();

popup1.show('TypeScript单例弹窗'); // 类型提示：content需为string
setTimeout(() => {
  popup2.hide(); // 操作同一弹窗实例
}, 2000);

// 错误：构造函数是私有的，禁止外部new
// const popup3 = new PopupManager(); // TS编译报错